package com.sumainfor.zuul.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.zuul.entity.SysCompanyEntity;
import com.sumainfor.zuul.service.SysCompanyService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author Jack Chen
 * @Create 2019-01-22 10:08
 * 签约公司
 */
@RestController
@RequestMapping("/sys/company")
public class SysCompanyController {

    @Autowired
    private SysCompanyService companyService;

    /**
     * 获得首页表格数据
     *
     * @param page
     * @param search
     * @return
     */
    @RequestMapping("/getTableList")
    public JsonResult<SysCompanyEntity> getTableList(Page page, String search) {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(search), "comp_name", search);
        Page pageResponse = companyService.selectPage(page, wrapper);
        return JsonResult.success(pageResponse);
    }

    @PostMapping("/addOrUpdate")
    public JsonResult add(@RequestBody Map<String, Object> params) {
        SysCompanyEntity entity = JSONObject.parseObject(JSONObject.toJSONString(params), SysCompanyEntity.class);
        if (entity == null) {
            return JsonResult.errorForEmpty();
        }
        //添加转账信息数据
        if (!params.containsKey("PayList")) {
            return JsonResult.errorForEmpty();
        }
        List<Map<String, Object>> payList = (List<Map<String, Object>>) params.get("PayList");
        boolean state = companyService.insertOrUpdate(entity);

        if (state == true) {
            if (entity.getCompId() == null) {
                state = companyService.insertPayData(payList, entity.getCompId());
            } else {
                state = companyService.updateAreaAccountByAreaId(payList, entity.getCompId());
            }
        }
        return state ? JsonResult.actionSuccess() : JsonResult.actionFailure();
    }

    @RequestMapping("/getInfoById")
    public JsonResult getInfoById(Integer id) {
        if (id == null) {
            return JsonResult.errorForEmpty();
        }
        return companyService.getInfoById(id);
    }


    @PostMapping("/deleteById")
    public JsonResult deleteById(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return JsonResult.errorForEmpty();
        }
        boolean state = companyService.deleteBatchIds(ids);

        for (int i = 0; i < ids.size(); i++) {
            companyService.deleteAreaAccountAllByAreaId(Integer.parseInt(ids.get(i)));
        }

        return state ? JsonResult.success("删除成功") : JsonResult.actionFailure();
    }

}
